{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0,  1,  2,  3],\n",
       "         [ 4,  5,  6,  7]],\n",
       "\n",
       "        [[ 8,  9, 10, 11],\n",
       "         [12, 13, 14, 15]]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(16).reshape(2,2,4)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1,  1,  1],\n",
       "        [ 2,  2,  2],\n",
       "        [ 3,  3,  3],\n",
       "        [ 4,  4,  4],\n",
       "        [ 5,  5,  5],\n",
       "        [ 6,  6,  6],\n",
       "        [ 7,  7,  7],\n",
       "        [ 8,  8,  8],\n",
       "        [ 9,  9,  9],\n",
       "        [10, 10, 10]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.as_strided(x, (10,3), (1, 0), 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1,  2,  3],\n",
       "        [ 2,  3,  4],\n",
       "        [ 3,  4,  5],\n",
       "        [ 4,  5,  6],\n",
       "        [ 5,  6,  7],\n",
       "        [ 6,  7,  8],\n",
       "        [ 7,  8,  9],\n",
       "        [ 8,  9, 10],\n",
       "        [ 9, 10, 11],\n",
       "        [10, 11, 12]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def as_strided(x: torch.Tensor, size: tuple, stride: tuple, offset: int):\n",
    "    y = torch.empty(size, dtype=x.dtype)\n",
    "    x = x.reshape([x.numel()])\n",
    "    y = y.reshape([y.numel()])\n",
    "    def get_index(size_index):\n",
    "        temp_size = y.numel()\n",
    "        indexs = torch.zeros(len(size), dtype=torch.int32)\n",
    "        for i, s in enumerate(size):\n",
    "            temp_size /= s\n",
    "            indexs[i] = size_index / temp_size\n",
    "            size_index = size_index % temp_size\n",
    "        ret = offset\n",
    "        for i in range(indexs.numel()):\n",
    "            ret += indexs[i] * stride[i]\n",
    "        return ret\n",
    "    for i in range(y.numel()):\n",
    "        index = get_index(i)\n",
    "        y[i]=x[index]\n",
    "    return y.reshape(size)\n",
    "as_strided(x, (10,3), (1, 1), 1)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
